package com.gxa.service.Impl;

import com.gxa.common.base.LayuiResultJson;
import com.gxa.common.base.Result;
import com.gxa.common.constant.Constants;
import com.gxa.domain.dto.CourseSaveDTO;
import com.gxa.domain.dto.CourseUpdateDTO;
import com.gxa.domain.entity.Course;
import com.gxa.domain.vo.CourseVO;
import com.gxa.mapper.CourseMapper;
import com.gxa.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public Result findCourseByCourseId(Integer courseId) {
        Course course = courseMapper.findCourseByCourseId(courseId);
        return Result.success(course);
    }

    @Override
    public Result addCourse(CourseSaveDTO courseSaveDTO) {
        int result = courseMapper.addCourse(courseSaveDTO);
        if(result > 0 ){
            return Result.success("添加成功！");
        }
        return Result.error();
    }

    @Override
    public Result updateCourse(CourseUpdateDTO courseUpdateDTO) {
        int result = courseMapper.updateCourse(courseUpdateDTO);
        if(result > 0 ){
            return Result.success("修改成功！");
        }
        return Result.error();
    }

    @Override
    public Result delCourseByCourseId(Integer courseId) {
        int result = courseMapper.delCourseByCourseId(courseId);
        if(result > 0 ){
            return Result.success("删除成功！");
        }
        return Result.error();
    }

    @Override
    public LayuiResultJson findCourseAll(Integer page, Integer limit) {
        LayuiResultJson layuiResultJson = new LayuiResultJson();
        Integer offset = (page - 1)*limit;
        Integer rows = limit;
        List<CourseVO> courses = courseMapper.findCourseList(offset,rows);
        layuiResultJson.setCode(Constants.OK);
        layuiResultJson.setMsg(Constants.MESSAGE_OK);
        Integer totalSize = courseMapper.countCourse();
        layuiResultJson.setCount(totalSize);
        layuiResultJson.setData(courses);
        return layuiResultJson;
    }

    @Override
    public LayuiResultJson search(String coursename, Integer page, Integer limit) {
        LayuiResultJson layuiResultJson = new LayuiResultJson();
        Integer offset = (page - 1)*limit;
        Integer rows = limit;
        List<CourseVO> courses = courseMapper.findCourseListByCondition(offset,rows,coursename);
        layuiResultJson.setCode(Constants.OK);
        layuiResultJson.setMsg(Constants.MESSAGE_OK);
        Integer totalSize = courseMapper.countCourseByCondition(coursename);
        layuiResultJson.setCount(totalSize);
        layuiResultJson.setData(courses);
        return layuiResultJson;
    }

}
